package com.mes.handler;

import com.mes.manufacture.data.EmesoJobOrder;
import com.mes.utils.EmesoMOJobOrderUtils;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import java.util.Date;
import java.util.Map;

/**
 * 作业订单强制完工
 */
public class CompelJobOrderHandler extends BaseActionHandler {
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		try {
			//设置最高权限
			OBContext.setAdminMode();
			JSONObject reqData = new JSONObject(data);
			// 获取作业订单
			String id = reqData.getString("recordId");
			EmesoJobOrder emojo = OBDal.getInstance().get(EmesoJobOrder.class, id);
//			if (emojo.isQuality() != null && emojo.isQuality()) {
//				emojo.setStatus("OK");
//			} else {
				emojo.setStatus("FINISHED");
//			}
			emojo.setFinish(true);
			emojo.setExeDateTo(new Date());
			OBDal.getInstance().save(emojo);
			//回写状态
			EmesoMOJobOrderUtils.syncUpdateOrderStatus();
			OBDal.getInstance().commitAndClose();
		} catch (Exception e) {
			e.printStackTrace();
			OBDal.getInstance().rollbackAndClose();
		}finally{
			OBContext.restorePreviousMode();
		}
		return jsonRep;
	}
}
